package com.sakura.system.controller.upload.readmodel;


import com.sakura.system.entity.upload.SrWebFileimageList;
import com.sakura.system.service.impl.uploadimpl.SrWebFileimageListServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
//@Controller
@RestController
@RequestMapping("/upload/web/read")
public class ReadImage {
//    读取配置文件
    @Value("${file.upload-folderimg}")
    private String uploadBaseDir;

    @Value("${file.upload-folderimg-file}")
    private String uploadBaseDirFile;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SrWebFileimageListServiceImpl service;


    /**
     * 读取文件夹数据
     */
    @GetMapping("/img")
    private Map<String, List<String>> readImages(@RequestParam("pawd") String pawd) {
        log.info(pawd);
        if (!pawd.equals("true")) {
            return null;
        }

        // 获取域名或IP和端口号
        String domain;
        if (request.getServerName().equals("localhost") || request.getServerName().equals("127.0.0.1")) {
            // 使用IP访问时，获取IP和端口号
            domain = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        } else {
            // 使用域名访问时，只获取域名
            domain = request.getScheme() + "://" + request.getServerName();
        }
//        log.info(domain);

        // 配置文件夹路径
        String configFolderPath = uploadBaseDir;
        String uploadBaseDirFile1 = uploadBaseDirFile;

        File folder = new File(configFolderPath);
        Map<String, List<String>> result = new HashMap<>();

        if (folder.isDirectory()) {
            File[] subfolders = folder.listFiles();

            for (File subfolder : subfolders) {
                if (subfolder.isDirectory()) {
//                    获取文件夹名称
                    String folderName = subfolder.getName();
//                    判断文件后缀
                    File[] imageFiles = subfolder.listFiles((dir, name) -> name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".jpeg"));
//                    log.info("判断文件后缀：");
//                    log.info(folderName);
//                    log.info(imageFiles.toString());
                    List<String> imageNames = new ArrayList<>();
                    for (File imageFile : imageFiles) {
                        String s = domain  + uploadBaseDirFile1 +'/' + folderName +'/'+ imageFile.getName();
//                        log.info(s);
                        imageNames.add(s);
                    }

                    result.put(folderName, imageNames);

                    SrWebFileimageList srWebFileimageList = new SrWebFileimageList();
                    srWebFileimageList.setName(folderName);
                    srWebFileimageList.setThumbnail(imageNames.get(0));
                    srWebFileimageList.setUrl(imageNames.toString());

                    //            保存到数据库
                    Integer save = service.save(null, srWebFileimageList);
                    if (!save.equals(true)){
                        return null;
                    }
                    }
            }
        }
//        log.info(result.toString());

        return result;
    }


}
